<?php

namespace app\api\controller;

use app\api\model\WxappPage;
use app\admin\model\Wxapp as WxappModel;
use app\common\model\AppointmentOrder as AppointmentOrderModel;
use think\db;
/**
 * 页面控制器
 * Class Index
 * @package app\api\controller
 */
class Page extends Controller
{
    /**
     * 页面数据
     * @param null $page_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index($page_id = null)
    {
        // 页面元素
        $data = WxappPage::getPageData($this->getUser(false), $page_id);

        $list = Db("wxapp")->where('is_recycle', 0)->where('is_delete', 0)->select();


        $model = new WxappModel;
        $store = $model->getStoreName($list);

        $token = input('token');
        if(!empty($token)){
            $user = $this->getUser();
            if ($user) {
                $wxapp_id_checked = Db('user')->where('user_id', $user['user_id'])->value('wxapp_id_checked');
                foreach ($store as $k => $v) {
                    $store[$k]['is_check'] = $v['id'] == $wxapp_id_checked ? 1 : 0;
                }
            }
        }
        foreach ($data['items'] as &$val) {
            if ($val['type'] == 'notice') {
                $val['params']['get_door'] = $store;
            } else if ($val['type'] == 'richText') {
                $val['params']['is_fu'] = 0;
                if (strpos($val['params']['content'], '服务卡') !== false) {
                    $val['params']['is_fu'] = 1;
                }
            }
        }

        $store = Db::name("setting")->where(array("key"=>"store","wxapp_id"=>10004))->find();
        $storeset = json_decode($store['values'],true);

        $data['opencheck']=$storeset['opencheck'];

        return $this->renderSuccess($data);
    }

    public function get_door()
    {
        $list = Db("wxapp")->where('is_recycle', 0)->where('is_delete', 0)->select();
        $model = new WxappModel;
        $store = $model->getStoreName($list);

        return $this->renderSuccess($store, 'success');
    }

    /*
   * 线下订单详情
   */
    public function offlineOrder()
    {
        $order_id = input('order_id');
        $orderModel = new AppointmentOrderModel;
        $detail = $orderModel->offlineDetail($order_id);
        return $this->renderSuccess(compact('detail'));
    }
}
